package de.eridea.learning.java.usermanagement.logic.pwdverification;

//import javax.persistence.Column;
//import javax.persistence.Embeddable;
//import javax.persistence.Transient;

//@Embeddable
public class PasswordDigest {
	// @Column(name = "PASSWORD_DIGEST")
	public final String digest;

	// @Column(name = "PASSWORD_HASHFUNCTION")
	public final String hashFunctionId;

	// @Transient
	private int cachedHashCode;

	// @Transient
	private boolean hashCodeGenerated = false;

	public PasswordDigest(String hashFunctionId, String digest) {
		if (hashFunctionId == null) {
			throw new IllegalArgumentException(
					"hashFunctionId must be non-null");
		}
		if (digest == null) {
			throw new IllegalArgumentException("digest must be non-null");
		}
		this.hashFunctionId = hashFunctionId;
		this.digest = digest;
	}

	public boolean equals(Object b) {
		boolean result = false;
		if (this.getClass().isInstance(b)) {
			result = this.hashFunctionId
					.equals(this.getClass().cast(b).hashFunctionId)
					&& this.digest.equals(this.getClass().cast(b).digest);
		}
		return result;
	}

	public int hashCode() {
		if (!hashCodeGenerated) {
			cachedHashCode = 2 * (hashFunctionId + digest).hashCode() + 3
					* hashFunctionId.hashCode() + 7 * digest.hashCode();
			hashCodeGenerated = true;
		}
		return cachedHashCode;
	}

	public String toString() {
		return hashFunctionId + " : " + digest;
	}
}
